<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div class="container">
    <h1>The Activity Page</h1>
    <!-- Button trigger modal -->
    <button type="button" class="btn btn-primary" data-toggle="modal"
            data-target="#myModal">创建新活动
    </button>
    <!-- Modal -->
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog"
         aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal"
                            aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                    <h4 class="modal-title" id="myModalLabel">添加活动</h4>
                </div>
                <div class="modal-body" id="saveFormId">
                    <form class="form-horizontal" th:action="@{/activity/doSaveActivity}" method="post">
                        <div class="form-group">
                            <label for="titleId" class="col-sm-2 control-label">标题</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control"  name="title" id="titleId" placeholder="title">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="categoryId" class="col-sm-2 control-label">类型</label>
                            <div class="col-sm-10">
                                <select id="categoryId" name="category" class="form-control">
                                    <option value="教育培训">教育培训</option>
                                    <option value="企业活动">企业活动</option>
                                    <option value="交友活动">交友活动</option>
                                </select>
                            </div>
                        </div>
                        <div class="form-group" >
                            <label for="startTimeId" class="col-sm-2 control-label">开始时间</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control form_datetime"
                                       name="startTime" id="startTimeId" placeholder="start time">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="endTimeId" class="col-sm-2 control-label">结束时间</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control form_datetime"
                                       name="endTime" id="endTimeId" placeholder="end time">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="remarkId" class="col-sm-2 control-label">备注</label>
                            <div class="col-sm-10">
           <textarea type="text" class="form-control"  rows="5" name="remark" id="remarkId">
            </textarea>
                            </div>
                        </div>
                    </form>


                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close
                    </button>
                    <button type="button" class="btn btn-primary" onclick="doSaveObject()">Save
                        changes
                    </button>
                </div>
            </div>
        </div>
    </div>
    <table class="table">
        <thead>
        <tr>
            <th>title</th>
            <th>Category</th>
            <th>StartTime</th>
            <th>EndTime</th>
            <th>State</th>
            <th>Operation</th>
        </tr>
        </thead>
        <tbody id="tbodyId">
        <tr><td colspan="6">数据正在加载中.......</td></tr>
        </tbody>
    </table>
</div>
</body>
<link rel="stylesheet" href="/css/bootstrap.min.css">
<script src="/js/jquery.min.js"></script>
<script src="/js/bootstrap.min.js"></script>
<script type="text/javascript">
    function doFindActivitys() {
        var url = "/activity/doFindActivitys"
        $.getJSON(url, function (result) {
            var tBody = $("#tbodyId");
            tBody.empty();//清空原有body内容
            for (var i = 0; i < result.length; i++) {//循环一次,迭代一行
                //构建当前行对象
                var tr = `<tr>
                          <td>${result[i].title}</td>
                          <td>${result[i].category}</td>
                          <td>${result[i].startTime}</td>
                          <td>${result[i].endTime}</td>
                          <td>${result[i].state == 1 ? '有效' : '无效'}</td>
                          <td><button type='button' class='btn btn-danger btn-sm' onclick='doDel(${result[i].id})'>delete</button></td>
                         </tr>`
                //将每一行的内容都追加tbody中
                tBody.append(tr);
            }
        });
    };

    function doSaveObject() {
        let url = "/activity/doSaveActivity";
        var params= $("form").serializeArray();
       // var param=`title=${params[0].value}&category=${params[1].value}&startTime=${params[2].value}&endTime=${params[3].value}&remark=${params[4].value}`
        var activity={};
        for(var tem in params){
            activity[params[tem].name]=params[tem].value;
        }

        $.post(url,activity,function (result) {
            alert(result);
            $('#myModal').modal('hide');
            doFindActivitys();
        })
        // $.ajax({
        //     type: "POST",
        //     url: url,
        //     data: JSON.stringify(json),
        //     success: function (result) {
        //         alert(result);
        //         $('#myModal').modal('hide');
        //         doFindActivitys();
        //     }
        // });

    }

    function doDel(id) {
        let url = `/activity/doDel?id=${id}`;
        $.ajax({
            type: "GET",
            url: url,
            success: function (result) {
                alert(result);
            }
        });

    }

    doFindActivitys();
</script>

</html>